package com.amigo.online.provider.ffmpeg.rocketmq;


import com.amigo.online.provider.ffmpeg.rocketmq.model.BaseMqSendModel;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class PushMessageAspect {

    private static final Logger log = LoggerFactory.getLogger(PushMessage.class);

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Pointcut("execution(* com.amigo.online.provider.ffmpeg.rocketmq.PushMessage.handleMessage(..))")
    public void aspect() {
    }

    /**
     * 环绕通知：
     * 环绕通知非常强大，可以决定目标方法是否执行，什么时候执行，执行时是否需要替换方法参数，执行完毕是否需要替换返回值。
     * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型
     *
     * @throws Throwable
     */
    @Around("aspect()")
    public void doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        BaseMqSendModel baseMqSendModel = (BaseMqSendModel) proceedingJoinPoint.getArgs()[0];
        log.info("这条message的key为:" + baseMqSendModel.getKeys());
        if (redisTemplate.opsForSet().isMember(PushMessage.REDIS_MQ_KEY, baseMqSendModel.getKeys())) {
            //这条消息是重复消费的
            log.info("重复消费了 ");
            return;
        }
        proceedingJoinPoint.proceed();//调用执行目标方法
        //在redis中存放这条消息已经被消费的标志，用来判断是否重复消费
        redisTemplate.opsForSet().add(PushMessage.REDIS_MQ_KEY, baseMqSendModel.getKeys());
        log.info("消息" + baseMqSendModel.getBeanName() + "消费成功");
    }
}
